﻿using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            RaisingStudio.Data.Providers.DataContext dc = new RaisingStudio.Data.Providers.DataContext();
            DataTable dataTable = dc.QueryForDataTable<RaisingStudio.Data.Entities.Issue>();
            this.GridView1.DataSource = dataTable;
            this.ViewState["dataTable"] = dataTable;
            this.GridView1.DataBind();
        }
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        DataTable dataTable = this.ViewState["dataTable"] as DataTable;
        if (dataTable != null)
        {
            if (e.NewEditIndex < dataTable.Rows.Count)
            {
                DataRow dataRow = dataTable.Rows[e.NewEditIndex];
                int issueID = (int)dataRow["IssueID"];
                string url = string.Format("~/Issue.aspx?{0}={1}", "IssueID", issueID);
                Response.Redirect(url);
            }
        }
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        DataTable dataTable = this.ViewState["dataTable"] as DataTable;
        if (dataTable != null)
        {
            if (e.RowIndex < dataTable.Rows.Count)
            {
                DataRow dataRow = dataTable.Rows[e.RowIndex];
                dataRow.Delete();

                RaisingStudio.Data.Providers.DataContext dc = new RaisingStudio.Data.Providers.DataContext();
                dc.UpdateDataTable<RaisingStudio.Data.Entities.Issue>(dataTable);

                this.GridView1.DataSource = dataTable;
                this.GridView1.DataBind();
            }
        }
    }
}
